var app = getApp()
const order = ['demo1', 'demo2', 'demo3']
const db = wx.cloud.database()  //创建数据库实例
Page({

  data: {
    scan: "",
    scrollH: 0,
    imgWidth: 0,
    loadingCount: 0,
    images: [],     //数据
    col1: [],     //左边
    col2: [],     //右边
    avatarUrl: 'images/wx.png', 
    userInfo: {},  //用户信息
    
    windowHeight:0,
    // nav
    selected: 0,
    list: ['全部', '时尚', '古风', '潮流', '运动', '休闲', '甜美', '韩风', '朋克',],
    name:"全部"
  },


  onLoad: function () {
    var that = this 
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.windowHeight)
          that.setData({
              windowHeight: res.windowHeight
          });
      }
    });
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
              console.log(res.userInfo.nickName)
              console.log(res.userInfo.avatarUrl)
              console.log(res.userInfo)
            }
          })
        }
      }
    })
    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let wh = res.windowHeight;
        let imgWidth = ww * 0.48;
        let scrollH = wh;

        this.setData({
          scrollH: scrollH,
          imgWidth: imgWidth
        });

        //加载首组图片
        this.loadImages();
      }
    })
  },

  focus: function (e) {
    wx.navigateTo({
      url: '../BlogSearch/BlogSearch',
    })
    
  },
  checkboxArr:function(e){
    var that=this
    console.log(e.currentTarget.id)    
    let index = e.currentTarget.id   
    console.log(index) 
    that.setData({ selected: index })   
   
    var checkboxArr=that.data.list
    var name=checkboxArr[index]
    that.setData({ name: name })   
    console.log(name)
    this.setData({
      images:[],
      col1: [],     //左边
      col2: [],     //右边
    })
    this.loadImages();
    // if(index=="0"){
    //   this.loadImages();
    // }else{
    //   db.collection('Bowman').where({
    //     dressingStyle: db.command.in([name]),
    //   })
    //   .get({
    //     success: function(res) {
    //       console.log(res.data)
    //       for (let i = 0; i < res.data.length; i++) {
    //         var id = "images[" + i + "].id"
    //         var title = "images[" + i + "].title"
    //         var nickName = "images[" + i + "].nickName"
    //         var avatarUrl = "images[" + i + "].avatarUrl"
    //         var imageurl = "images[" + i + "].imageurl"
  
    //         that.setData({
    //           [id]: res.data[i]._id,
    //           [title]: res.data[i].title,
    //           [nickName]: res.data[i].nickName,
    //           [avatarUrl]: res.data[i].avatarUrl,
    //           [imageurl]: res.data[i].imageurl[0]
    //         })
  
    //       }
    //     }
        
    //   })
    //   var images = that.data.images
    //   console.log(images);
    //   let baseId = "img-" + (+new Date());
  
    //   for (let i = 0; i < images.length; i++) {
    //     images[i].id = baseId + "-" + i;
    //   }
  
    //   this.setData({
    //     loadingCount: images.length,
    //     images: images
    //   });
    // }
    
    
  },

  onImageLoad: function (e) {
    // this.setData({
    //   col1: [],     //左边
    //   col2: [],     //右边
    // })
    let imageId = e.currentTarget.id;
    let oImgW = e.detail.width;         //图片原始宽度
    let oImgH = e.detail.height;        //图片原始高度
    let imgWidth = this.data.imgWidth;  //图片设置的宽度
    let scale = imgWidth / oImgW;        //比例计算
    let imgHeight = oImgH * scale;      //自适应高度
    
    let images = this.data.images;
    let imageObj = null;

    

    for (let i = 0; i < images.length; i++) {
      let img = images[i];
      if (img.id === imageId) {
        imageObj = img;
        break;
      }
    }
    imageObj.height =100;
    imageObj.height = imgHeight;

    let loadingCount = this.data.loadingCount - 1;
    let col1 = this.data.col1;
    let col2 = this.data.col2;

    //判断当前图片添加到左列还是右列

    if (col1.length <= col2.length) {
      col1.push(imageObj);
    } else {
      col2.push(imageObj);
    }

    let data = {
      loadingCount: loadingCount,
      col1: col1,
      col2: col2

    };

    //当前这组图片已加载完毕，则清空图片临时加载区域的内容
    if (!loadingCount) {
      data.images = [];
    }
    console.log(this.data.col1)
    this.setData(data);
  },

  loadImages: function () {
    let images;
    let name = this.data.name
    let a ="dressingStyle: db.command.in([name])"
    var that = this
    let b = db.command.in
    if(name == "全部"){
      b = db.command.nin
    }
    db.collection('Bowman').where({
      dressingStyle: b([name])
    }).get({
      success: function (res) {
          console.log(res)
        for (let i = 0; i < res.data.length; i++) {
          var id = "images[" + i + "].id"
          var title = "images[" + i + "].title"
          var nickName = "images[" + i + "].nickName"
          var avatarUrl = "images[" + i + "].avatarUrl"
          var imageurl = "images[" + i + "].imageurl"

          that.setData({
            [id]: res.data[i]._id,
            [title]: res.data[i].title,
            [nickName]: res.data[i].nickName,
            [avatarUrl]: res.data[i].avatarUrl,
            [imageurl]: res.data[i].imageurl[0]
          })

        }
      }
    })
    images = that.data.images
    console.log(images);
    let baseId = "img-" + (+new Date());

    for (let i = 0; i < images.length; i++) {
      images[i].id = baseId + "-" + i;
    }

    this.setData({
      loadingCount: images.length,
      images: images
    });
  }

})
